import { existsSync, readdirSync, statSync, unlinkSync, rmSync, writeFileSync as _writeFileSync, mkdirSync as _mkdirSync } from 'fs';

export function delDir(p){
  if(!existsSync(p)) return;
  var arr = readdirSync(p);
  for(var i in arr){
    //读取文件信息，以便于判断是否是一个文件或目录
    var stats=statSync(p+'/'+arr[i]);
    if(stats.isFile()){
      //判断为真，是文件则执行删除文件
      unlinkSync(p+'/'+arr[i]);
    }else{
      //判断为假就是文件夹，就调用自己，递归的入口
      delDir(p+'/'+arr[i]);
    }
  }
  //删除空目录
  rmSync(p, {
    recursive: true
  });
}

export function writeFileSync(path, content) {
  _writeFileSync(path, content, {
    encoding: "utf-8"
  })
}

export function mkdirSync(dir) {
  if(!existsSync(dir)) _mkdirSync(dir, {
    recursive: true
  })
}
